package com.dataaccess.dataaccess.web.service;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.HexUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.PananSmockNumber;
import com.dataaccess.dataaccess.model.SmokeAlarmDeviceDataChanged;
import com.dataaccess.dataaccess.model.SmokeNormalDeviceDataChanged;
import com.dataaccess.dataaccess.model.Smokeanpaiceshi;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.mybatisplus.QueryWrapper;
import com.dataaccess.dataaccess.support.utils.ByteUtil;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hanwei.weiguo.jiKaiDaSmoke.plugin.AdaptorImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

import static java.lang.Integer.parseInt;

@Service
@Slf4j
public class DeviceAboatSmokeService extends BaseService {

    @Resource
    public MyLogger logger;
    @Resource
    private SmokeAlarmDeviceDataChangedMapper smokeAlarmDeviceDataChangedMapper;
    @Resource
    private SmokeNormalDeviceDataChangedMapper smokeNormalDeviceDataChangedMapper;
    @Resource
    private NewSmokeAlarmDeviceDataChangedMapper newSmokeAlarmDeviceDataChangedMapper;
    @Resource
    private smokeanpaiceshiMapper smokeanpaiceshiMapper;
    @Resource
    private PananSmockNumberMapper pananSmockNumberMapper;
    @Resource
    private AsyncService asyncService;

    /**
     * 订阅--调用相关接口
     *
     * @param
     * @return
     */
    public RespEntity getInfo(JSONObject jsonParam) {

        return RespEntity.success();
    }

    /**
     * 回调地址--设备信息变化，订阅后推送设备信息变化通知
     *
     * @param
     * @return
     */
    public RespEntity deviceInfoChanged(JSONObject jsonParam) {

        return RespEntity.success();
    }

    /**
     * 智慧独立烟感对接
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     *
     * @param
     * @return
     */
    public RespEntity deviceDataChanged(JSONObject jsonParam) throws Exception {
        //处理支持form-data和json两种方式
        //jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        logger.info("+++++++++++SmokeData+++++" + jsonParam);
        try {
            if (!jsonParam.containsKey("termId")) {
                if (jsonParam.containsKey("eventContent") || jsonParam.getJSONObject("payload").containsKey("imei")) {
                    //new505烟感
                    new505Smoke(jsonParam);
                } else if (jsonParam.containsKey("payload")) {
                    if (jsonParam.getJSONObject("payload").containsKey("serviceData")) {
                        //派安烟感
                        panAnSmoke(jsonParam);
                    } else if (jsonParam.getJSONObject("payload").containsKey("APPdata")) {
                        //old505烟感
                        old505Smoke(jsonParam);
                    }
                }
            } else {
                //035烟感
                oldSmoke(jsonParam);
            }
        } catch (Exception e) {
            logger.info("+++++++++++Exception+++++" + e);
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 汉威035烟感
     * @Date Created in 14:41 2020/11/25
     * @Param [jsonParam]
     */
    public RespEntity oldSmoke(JSONObject jsonParam) {
        logger.info("+++++++++++035SmokeData+++++" + jsonParam);
        JSONObject payload = jsonParam.getJSONObject("payload");
        //设备号
        String number = jsonParam.getStr("deviceCode");
        //祥云设备id
        String termId = jsonParam.getStr("termId");
        //报警状态 0无报警\1烟雾报警
        JSONObject batchData = payload.getJSONObject("batchData");
        String smokeStatus = batchData.getStr("smokeStatus");
        //采集板电池状态
        String acqBoaBatStatus = batchData.getStr("acqBoaBatStatus");
        //采集板电池电量
        String acqBoaBatVal = batchData.getStr("acqBoaBatVal");
        //通讯板电池状态
        String commBoaBatStatus = batchData.getStr("commBoaBatStatus");
        //通讯板电池电量
        String commBoaBatVal = batchData.getStr("commBoaBatVal");
        //烟雾浓度
        String smokeSignVal = batchData.getStr("smokeSignVal");
        //报警时间
        String date = jsonParam.getStr("eventTime");
        //信号强度
        int modelSignVal = (batchData.getInt("modelSignVal") + 113) / 2;
        //报警类型名称
        String alarmTypeName = "";
        //消防数据
        String alarmStr = "";
        //报警内容
        String content = "";
        //报警标识
        boolean flag = false;
        if ("1".equals(smokeStatus)) {
            alarmTypeName = "烟雾报警";
            alarmStr = "7001" + ";;" + alarmTypeName;
            content = alarmTypeName;
            flag = true;
        } else if ("4".equals(smokeStatus)) {
            alarmTypeName = "信号异常报警";
            alarmStr = "7001" + ";;" + alarmTypeName;
            content = alarmTypeName;
            flag = true;
        } else if ("5".equals(smokeStatus)) {
            alarmTypeName = "自检报警";
            alarmStr = "7001" + ";;" + alarmTypeName;
            content = alarmTypeName;
            flag = true;
        } else if ("7".equals(smokeStatus)) {
            alarmTypeName = "拆除报警";
            alarmStr = "7001" + ";;" + alarmTypeName;
            content = alarmTypeName;
            flag = true;
        } else if ("0".equals(acqBoaBatStatus)) {
            alarmTypeName = "采集板低电量报警";
            content = alarmTypeName + " 采集板电池电量:" + acqBoaBatVal;
            alarmStr = "7001" + ";;" + alarmTypeName;
            flag = true;
        } else if ("0".equals(commBoaBatStatus)) {
            alarmTypeName = "通讯板低电量报警";
            content = alarmTypeName + " 通讯板电池电量:" + commBoaBatVal;
            alarmStr = "7001" + ";;" + alarmTypeName;
            flag = true;
        }
        String alarmStatus = "";
        //平时数据
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        //烟雾报警
        if (flag) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("number", number);
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmStr", alarmStr);
            jsonObject.put("alarmtime", date);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            jsonObject.put("termId", termId);
            //报警原始数据存储
            SmokeAlarmDeviceDataChanged smokeAlarmDeviceDataChanged = new SmokeAlarmDeviceDataChanged();
            smokeAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            smokeAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            smokeAlarmDeviceDataChangedMapper.insert(smokeAlarmDeviceDataChanged);
            //rest
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
            alarmStatus = "报警";
        } else {
            alarmStatus = "正常";
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(getNormalItem("烟雾浓度变化", smokeSignVal));
        jsonArray.add(getNormalItem("信号", modelSignVal));
        jsonArray.add(getNormalItem("状态", alarmStatus));
        jsonArray.add(getNormalItem("电量", acqBoaBatVal.substring(0, acqBoaBatVal.length() - 1)));
        jsonParamN.put("interfaceType", "2");
        jsonParamN.put("deviceClassify", "3");
        jsonParamN.put("time", date);
        jsonParamN.put("paraminfo", jsonArray);
        jsonParamN.put("termId", termId);
        //正常原始数据存储
        SmokeNormalDeviceDataChanged smokeNormalDeviceDataChanged = new SmokeNormalDeviceDataChanged();
        smokeNormalDeviceDataChanged.setId(UUID.randomUUID().toString());
        smokeNormalDeviceDataChanged.setJsondata(jsonParam.toString());
        smokeNormalDeviceDataChangedMapper.insert(smokeNormalDeviceDataChanged);
        //烟感设备绑定祥云设备id
        fireFightingiot(jsonParamN, "firefightingiot", "termIdUpdate", "device");
        //正常数据
        //大数据
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");

        return RespEntity.success();

    }

    public JSONObject getNormalItem(String name, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("value", value);
        return jsonObject;
    }

    /**
     * @Author YangYu
     * @Description 汉威new505烟感
     * @Date Created in 14:41 2020/11/25
     * @Param [jsonParam]
     */
    public RespEntity new505Smoke(JSONObject jsonParam) throws Exception {
        logger.info("+++++++++++new505SmokeData+++++" + jsonParam);
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警类型
        String alarmType = "";
        //温度值
        String temperature = "";
        if (jsonParam.containsKey("eventContent")) {
            //报警类型
            JSONObject eventContent = jsonParam.getJSONObject("eventContent");
            alarmType = eventContent.getStr("payload");
            //温度值
            temperature = eventContent.getStr("temperature");
        }
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        //报警标识
        boolean flag = false;
        if ("55AA00015A".equals(alarmType)) {
            alarmTypeName = "烟雾报警";
            content = alarmTypeName;
            flag = true;
        }
        if ("55AA00025A".equals(alarmType)) {
            alarmTypeName = "低电量报警";
            content = alarmTypeName;
            flag = true;
        }
        if ("55AA00045A".equals(alarmType)) {
            alarmTypeName = "拆除报警";
            content = alarmTypeName;
            flag = true;
        }
        if ("55AA00055A".equals(alarmType)) {
            alarmTypeName = "自检报警";
            content = alarmTypeName;
            flag = true;
        }
        if ("55AA00075A".equals(alarmType)) {
            alarmTypeName = "烟感故障";
            content = alarmTypeName;
            flag = true;
        }
        if ("55AA00085A".equals(alarmType)) {
            alarmTypeName = "烟感温度报警";
            content = alarmTypeName + " 温度值:" + temperature + "℃";
            flag = true;
        }
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        //为报警
        if (flag) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("number", number);
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            //报警原始数据存储
            SmokeAlarmDeviceDataChanged smokeAlarmDeviceDataChanged = new SmokeAlarmDeviceDataChanged();
            smokeAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            smokeAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            smokeAlarmDeviceDataChangedMapper.insert(smokeAlarmDeviceDataChanged);
            //rest
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }

        //消防数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "烟雾浓度变化");
        jsonObject.put("value", null);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        //平时数据
        jsonParamN.put("interfaceType", "2");
        jsonParamN.put("deviceClassify", "3");
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        //正常原始数据存储
        SmokeNormalDeviceDataChanged smokeNormalDeviceDataChanged = new SmokeNormalDeviceDataChanged();
        smokeNormalDeviceDataChanged.setId(UUID.randomUUID().toString());
        smokeNormalDeviceDataChanged.setJsondata(jsonParam.toString());
        smokeNormalDeviceDataChangedMapper.insert(smokeNormalDeviceDataChanged);
        //正常数据
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();

    }

    /**
     * @Author XuLumin
     * @Description 海康烟感(电信)
     * @Date Created in 08:57 2024/1/25
     * @Param [jsonParam]
     */
    public RespEntity haiKang(JSONObject jsonParam) throws Exception {
        try {

            DecimalFormat df = new DecimalFormat("0.00");
            //数据类型
            String messageType = jsonParam.getStr("messageType");
            if (!messageType.equals("dataReport")) {
                return RespEntity.success();
            }
            String time = contextLoads((jsonParam.getLong("timestamp")));
            // 拿数据
            JSONObject data = getHkData(jsonParam);
            // 设备号
            String number = hexToAscii(data.getStr("byImei"));
            // 命令
            String command = data.getStr("byMessageId");
            // 消息
            String msgType = data.getStr("byMsgType");

            // 报警类型名称
            String alarmTypeName = "";
            // 报警内容
            String content = "";
            // 烟雾浓度
            double smokeConcentration = 0;
            //温度
            Integer temperature = 0;
            //电池电量
            double battery = 0;

            boolean flag = false;
            //转发数据
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("number", number);

            // 判断业务类型
            if ("07".equals(command)) { // 设备注册
                jsonObject.put("state", "1");
                asyncService.updateDeviceCurrentState(jsonObject);
                // QCCID号
                String qccId = data.getStr("byQccId");
                jsonObject.put("iccid", qccId);
                this.asyncService.update_ICCID(jsonObject);
                return RespEntity.success();

            } else if ("02".equals(command)) { // 设备报警
                String bodyInfo[] = {"bodyInfo1", "bodyInfo2", "bodyInfo3", "bodyInfo4"};
                // 烟雾浓度
                String smokeConcentrationStr = data.getJSONObject("bodyInfo1").getStr("wParamValue");
                smokeConcentration = hexToInt(smokeConcentrationStr) / 200.0;
                // 温度
                String temperatureStr = data.getJSONObject("bodyInfo2").getStr("wParamValue");
                temperature = hexToInt(temperatureStr) / 10;
                // 湿度
                String humidnessStr = data.getJSONObject("bodyInfo3").getStr("wParamValue");
                Double humidness = Double.valueOf(df.format(hexToInt(humidnessStr) / 100.00));
                // 水汽浓度
                String WaterVaporStr = data.getJSONObject("bodyInfo4").getStr("wParamValue");

                // 遍历数据体
                for (String body : bodyInfo) {
                    if ("0064".equals(data.getJSONObject(body).getStr("wEventType"))) {
                        String enentValue = hexToBinaryString(data.getJSONObject(body).getStr("wEventValue"));
                        char c = enentValue.charAt(7);
                        if (enentValue.charAt(7) == '1') {
                            alarmTypeName = "烟雾报警";
                        } else if (enentValue.charAt(6) == '1') {
                            alarmTypeName = "燃气报警";
                        } else if (enentValue.charAt(4) == '1') {
                            alarmTypeName = "温度报警";
                        }
                    }
                }
                content = alarmTypeName + " 烟雾浓度:" + smokeConcentration + " 温度:" + temperature;
                flag = true;
                //异步更新设备状态
                jsonObject.put("state", "2");
                asyncService.updateDeviceCurrentState(jsonObject);
            } else if ("01".equals(command)) { // 设备心跳
                // 烟雾浓度
                String smokeConcentrationStr = data.getJSONObject("bodyInfo2").getStr("wParamValue");
                smokeConcentration = hexToInt(smokeConcentrationStr) / 200.0;
                // 电池电量
                String batteryStr = data.getJSONObject("bodyInfo1").getStr("wParamValue");
                battery = Double.valueOf(df.format(hexToInt(batteryStr) / 30.0));
                // 温度
                String temperatureStr = data.getJSONObject("bodyInfo4").getStr("wParamValue");
                temperature = hexToInt(temperatureStr) / 10;
                jsonObject.put("state", "1");
                asyncService.updateDeviceCurrentState(jsonObject);
            } else if ("05".equals(command)) { // 故障
                String enentValue = hexToBinaryString(data.getJSONObject("bodyInfo1").getStr("wEventValue"));
                //
                if (enentValue.charAt(7) == '1') {
                    alarmTypeName = "寿命超期故障";
                } else if (enentValue.charAt(5) == '1') {
                    alarmTypeName = "低电压故障";
                } else if (enentValue.charAt(3) == '1') {
                    alarmTypeName = "迷宫污染故障";
                }
                flag = true;
                jsonObject.put("state", "4");
                asyncService.updateDeviceCurrentState(jsonObject);
            }
            //电池低于2.7v状态更新为故障
            if (battery < 2.7) {
                jsonObject.put("state", "4");
                asyncService.updateDeviceCurrentState(jsonObject);
            }

            if (flag) {
                jsonObject.put("deviceClassify", "3");
                jsonObject.put("interfaceType", "1");
                jsonObject.put("alarmtime", time);
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                //rest
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            }

            JSONObject jsonParamN = new JSONObject();
            //消防数据
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(getNormalItem("烟雾浓度变化", smokeConcentration));
            jsonArray.add(getNormalItem("温度", temperature));
            jsonArray.add(getNormalItem("电量", battery));
            //平时数据
            jsonParamN.put("interfaceType", "2");
            jsonParamN.put("deviceClassify", "3");
            jsonParamN.put("number", number);
            jsonParamN.put("time", time);
            jsonParamN.put("paraminfo", jsonArray);
            //正常数据
            jsonParamN.put("dataType", "2");
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
            fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return RespEntity.success();
    }


    /**
     * @Author YangYu
     * @Description 塞特威尔烟感
     * @Date Created in 15:44 2022/8/8
     * @Param [jsonParam]
     */

    public RespEntity siterWell(JSONObject jsonParam) throws Exception {
        if (!jsonParam.containsKey("payload")) {
            return RespEntity.fail("数据格式不正确!");
        }
        if (jsonParam.getStr("productId").equals("15291781") || jsonParam.getStr("productId").equals("17035858")) {
            return siterWellGS557N(jsonParam);
        } else {
            return siterwellTelecommunicationsSmoke(jsonParam);
        }
    }

    /**
     * @Author YangYu
     * @Description 赛特威尔烟感（电信）
     * @Date Created in 14:14 2021/12/16
     * @Param [jsonParam]
     */

    public RespEntity siterwellTelecommunicationsSmoke(JSONObject jsonParam) throws Exception {

        JSONObject payload = jsonParam.getJSONObject("payload");
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //烟雾浓度
        String smokeValue = payload.getStr("smoke_value");
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        if (payload.containsKey("smoke_state")) {
            //烟雾报警状态 0报警 1解除报警
            String smokeState = payload.getStr("smoke_state");
            if (smokeState.equals("0")) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("deviceClassify", "3");
                jsonObject.put("number", number);
                jsonObject.put("interfaceType", "1");
                jsonObject.put("alarmtime", time);
                jsonObject.put("alarmTypeName", "烟雾报警");
                jsonObject.put("content", "烟雾报警 报警浓度:" + smokeValue + "%");
                //rest
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
                //异步更新设备状态
                jsonObject.put("state", "2");
                asyncService.updateDeviceCurrentState(jsonObject);
            }
        } else {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }

        //消防数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "烟雾浓度变化");
        jsonObject.put("value", null);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        //平时数据
        jsonParamN.put("interfaceType", "2");
        jsonParamN.put("deviceClassify", "3");
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        //正常原始数据存储
        SmokeNormalDeviceDataChanged smokeNormalDeviceDataChanged = new SmokeNormalDeviceDataChanged();
        smokeNormalDeviceDataChanged.setId(UUID.randomUUID().toString());
        smokeNormalDeviceDataChanged.setJsondata(jsonParam.toString());
        smokeNormalDeviceDataChangedMapper.insert(smokeNormalDeviceDataChanged);
        //正常数据
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 赛特威尔烟感（电信-GS557N）
     * @Date Created in 15:06 2022/8/8
     * @Param [jsonParam]
     */

    public RespEntity siterWellGS557N(JSONObject jsonParam) throws Exception {

        //解析数据
        JSONObject serviceData = jsonParam.getJSONArray("payload").getJSONObject(0).getJSONObject("serviceData");
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //指令数据
        String custom_msg = serviceData.getStr("Custom_Msg");
        //公共状态
        String public_status = hexToBinaryString(custom_msg.substring(2, 4));
        //烟感状态
        String smoke_status = hexToBinaryString(custom_msg.substring(4, 6));
        //烟雾浓度
        Integer r_value = parseInt(custom_msg.substring(8, 10), 16);
        //报警类型
        String alarmTypeName = "";
        //报警内容
        String content = "";
        boolean flag = false;
        if (public_status.charAt(0) == '1') {
            alarmTypeName = "拆除报警";
            content = "防拆触发警告";
            flag = true;
        } else if (public_status.charAt(3) == '1') {
            alarmTypeName = "低电量报警";
            content = "低压警告";
            flag = true;
        } else if (public_status.charAt(6) == '1') {
            alarmTypeName = "自检报警";
            content = "自检报警";
            flag = true;
        } else if (smoke_status.charAt(1) == '1') {
            alarmTypeName = "烟雾报警";
            content = "烟雾报警 浓度:" + r_value + "%";
            flag = true;
        } else if (smoke_status.charAt(2) == '1') {
            alarmTypeName = "烟雾报警";
            content = "烟雾报警静音 浓度:" + r_value + "%";
            flag = true;
        } else if (smoke_status.charAt(3) == '1') {
            alarmTypeName = "烟感故障";
            content = "烟感故障";
            flag = true;
        } else if (smoke_status.charAt(4) == '1') {
            alarmTypeName = "烟感故障";
            content = "烟雾传感器故障免打扰";
            flag = true;
        }
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        jsonParamN.put("deviceClassify", "3");
        if (flag) {
            jsonParamN.put("interfaceType", "1");
            jsonParamN.put("alarmtime", time);
            jsonParamN.put("alarmTypeName", alarmTypeName);
            jsonParamN.put("content", content);
            //rest
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonParamN.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonParamN);
        } else {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }
        jsonParamN.put("time", time);
        //消防数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "烟雾浓度变化");
        jsonObject.put("value", r_value);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonParamN.put("paraminfo", jsonArray);
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 十六进制转二进制字符串
     * @Date Created in 14:07 2021/11/24
     * @Param [hex]
     */

    public static String hexToBinaryString(String hex) {
        String str = Integer.toBinaryString(parseInt(hex, 16));
        if (str.length() == 1) {
            str = "0000000" + str;
        } else if (str.length() == 2) {
            str = "000000" + str;
        } else if (str.length() == 3) {
            str = "00000" + str;
        } else if (str.length() == 4) {
            str = "0000" + str;
        } else if (str.length() == 5) {
            str = "000" + str;
        } else if (str.length() == 6) {
            str = "00" + str;
        } else if (str.length() == 7) {
            str = "0" + str;
        }
        return str;
    }

    /**
     * @Author YangYu
     * @Description 思维智安烟感
     * @Date Created in 16:25 2021/7/15
     * @Param [jsonParam]
     */

    public RespEntity thinKingZhiAnSmoke(JSONObject jsonParam) throws Exception {

        JSONObject payload = jsonParam.getJSONObject("payload");
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //报警状态 0烟雾报警 1烟雾报警消除
        String alarmState = payload.getStr("smoke_state");
        //烟雾报警浓度 %比
        String smokeValue = payload.getStr("smoke_value");
        //电池电量
        String batteryValue = payload.getStr("battery_value");
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        //报警标识
        boolean flag = false;
        if ("0".equals(alarmState)) {
            alarmTypeName = "烟雾报警";
            content = alarmTypeName + " 浓度:" + smokeValue + "%";
            flag = true;
        }
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        //为报警
        if (flag) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("number", number);
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            //报警原始数据存储
            SmokeAlarmDeviceDataChanged smokeAlarmDeviceDataChanged = new SmokeAlarmDeviceDataChanged();
            smokeAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            smokeAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            smokeAlarmDeviceDataChangedMapper.insert(smokeAlarmDeviceDataChanged);
            //rest
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }

        //消防数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "烟雾浓度变化");
        jsonObject.put("value", null);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        //平时数据
        jsonParamN.put("interfaceType", "2");
        jsonParamN.put("deviceClassify", "3");
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        //正常原始数据存储
        SmokeNormalDeviceDataChanged smokeNormalDeviceDataChanged = new SmokeNormalDeviceDataChanged();
        smokeNormalDeviceDataChanged.setId(UUID.randomUUID().toString());
        smokeNormalDeviceDataChanged.setJsondata(jsonParam.toString());
        smokeNormalDeviceDataChangedMapper.insert(smokeNormalDeviceDataChanged);
        //正常数据
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 汉威old505烟感
     * @Date Created in 14:41 2020/11/25
     * @Param [jsonParam]
     */
    public RespEntity old505Smoke(JSONObject jsonParam) throws Exception {
        logger.info("+++++++++++old505SmokeData+++++" + jsonParam);
        //payload
        JSONObject payload = jsonParam.getJSONObject("payload");
        //base64报警数据转十六进制
        String appdata = HexUtil.encodeHexStr(Base64Decoder.decode(payload.getStr("APPdata")));
        //报警内容转json
        AdaptorImpl adaptor = new AdaptorImpl();
        ObjectNode data = adaptor.decode(appdata.getBytes());
        //报警数据
        JsonNode alarmData = data.get("data");
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警类型
        String alarmType = alarmData.get("alarm_type").asText();
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        //报警标识
        boolean flag = false;
        if ("5".equals(alarmType)) {
            alarmTypeName = "烟雾报警";
            content = alarmTypeName;
            flag = true;
        }
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        //为报警
        if (flag) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("number", number);
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            //报警原始数据存储
            SmokeAlarmDeviceDataChanged smokeAlarmDeviceDataChanged = new SmokeAlarmDeviceDataChanged();
            smokeAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            smokeAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            smokeAlarmDeviceDataChangedMapper.insert(smokeAlarmDeviceDataChanged);
            //rest
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }

        //消防数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "烟雾浓度变化");
        jsonObject.put("value", null);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        //平时数据
        jsonParamN.put("interfaceType", "2");
        jsonParamN.put("deviceClassify", "3");
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        //正常原始数据存储
        SmokeNormalDeviceDataChanged smokeNormalDeviceDataChanged = new SmokeNormalDeviceDataChanged();
        smokeNormalDeviceDataChanged.setId(UUID.randomUUID().toString());
        smokeNormalDeviceDataChanged.setJsondata(jsonParam.toString());
        smokeNormalDeviceDataChangedMapper.insert(smokeNormalDeviceDataChanged);
        //正常数据
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    //派安烟感对接
    public RespEntity panAnSmoke(JSONObject jsonParam) throws Exception {
        logger.info("+++++++++++派安SmokeData+++++" + jsonParam);
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //报警
        String AlarmContent = jsonParam.getJSONObject("payload").getJSONObject("serviceData").getStr("content");
        //指令（报警故障03）
        String instructions = AlarmContent.substring(0, 2);
        if (instructions.equals("03")) {
            //指令类型
            String instructionsType = AlarmContent.substring(2, 4);
            //报警类型（）
            String alarmType = AlarmContent.substring(4, 6);
            //报警类型（）
            String alarmTypeName = "";
            //警告等级
            String alarmLevel = AlarmContent.substring(6, 8);
            //警告内容
            String alarmContent = AlarmContent.substring(8, 10);
            //数据top
            String top = AlarmContent.substring(10, 12);
            //数据bottom
            String bottom = AlarmContent.substring(12, 14);
            //数据类型
            String dataType = AlarmContent.substring(14, 16);
            //信号强度
            String signalIntensity = AlarmContent.substring(16, 18);
            //电量
            String electricityQuantity = AlarmContent.substring(18, 20);
            //报警内容
            String content = "";

            //报警标识
            boolean flag = false;
            if (alarmContent.equals("03")) {
                alarmTypeName = "烟雾报警";
                content = alarmTypeName;
                flag = true;
            }
            if (alarmContent.equals("4f")) {
                alarmTypeName = "自检报警";
                content = alarmTypeName;
                flag = true;
            }

            //报警+测试
            if (flag) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("deviceClassify", "3");
                jsonObject.put("number", number);
                jsonObject.put("interfaceType", "1");
                jsonObject.put("alarmtime", time);
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                //报警原始数据存储
                SmokeAlarmDeviceDataChanged smokeAlarmDeviceDataChanged = new SmokeAlarmDeviceDataChanged();
                smokeAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
                smokeAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
                smokeAlarmDeviceDataChangedMapper.insert(smokeAlarmDeviceDataChanged);
                //异步更新设备状态
                jsonObject.put("state", "2");
                asyncService.updateDeviceCurrentState(jsonObject);
                //rest
                return fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            }
        } else if (instructions.equals("02")) {
            //指令类型
            String instructionsType = AlarmContent.substring(2, 4);
            if (instructionsType.equals("01")) {
                String length = AlarmContent.substring(4, 6);
                int lengthDec = (int) Long.parseLong(length, 16);
                int deviceIdstart = 6;
                int deviceIdend = 6 + (lengthDec * 2);
                //AscII表
                HashMap<String, String> hashMap = this.AscIITable();
                //设备ID
                String deviceId = AlarmContent.substring(deviceIdstart, deviceIdend);
                String deviceIdJiXiAfter = "";
                for (int i = 0; i <= (lengthDec * 2) - 2; i = i + 2) {
                    String sub = deviceId.substring(i, i + 2);
                    deviceIdJiXiAfter += hashMap.get(sub);
                }


                int deviceTypend = deviceIdend + 2;
                String deviceType = AlarmContent.substring(deviceIdend, deviceTypend);
                int iotCardNumberend = (deviceTypend + 20 * 2);
                String iotCardNumber = AlarmContent.substring(deviceTypend, iotCardNumberend);

                String one = iotCardNumber.substring(0, 2);
                String oneValue = hashMap.get(one);
                String two = iotCardNumber.substring(2, 4);
                String twoValue = hashMap.get(two);
                String three = iotCardNumber.substring(4, 6);
                String threeValue = hashMap.get(three);
                String four = iotCardNumber.substring(6, 8);
                String fourValue = hashMap.get(four);
                String five = iotCardNumber.substring(8, 10);
                String fiveValue = hashMap.get(five);
                String six = iotCardNumber.substring(10, 12);
                String sixValue = hashMap.get(six);
                String seven = iotCardNumber.substring(12, 14);
                String sevenValue = hashMap.get(seven);
                String eight = iotCardNumber.substring(14, 16);
                String eightValue = hashMap.get(eight);
                String nine = iotCardNumber.substring(16, 18);
                String nineValue = hashMap.get(nine);
                String ten = iotCardNumber.substring(18, 20);
                String tenValue = hashMap.get(ten);
                String one1 = iotCardNumber.substring(20, 22);
                String one1Value = hashMap.get(one1);
                String two1 = iotCardNumber.substring(22, 24);
                String two1Value = hashMap.get(two1);
                String three1 = iotCardNumber.substring(24, 26);
                String three1Value = hashMap.get(three1);
                String four1 = iotCardNumber.substring(26, 28);
                String four1Value = hashMap.get(four1);
                String five1 = iotCardNumber.substring(28, 30);
                String five1Value = hashMap.get(five1);
                String six1 = iotCardNumber.substring(30, 32);
                String six1Value = hashMap.get(six1);
                String seven1 = iotCardNumber.substring(32, 34);
                String seven1Value = hashMap.get(seven1);
                String eight1 = iotCardNumber.substring(34, 36);
                String eight1Value = hashMap.get(eight1);
                String nine1 = iotCardNumber.substring(36, 38);
                String nine1Value = hashMap.get(nine1);
                String ten1 = iotCardNumber.substring(38, 40);
                String ten1Value = hashMap.get(ten1);
                String value = oneValue + twoValue + threeValue + fourValue + fiveValue + sixValue + sevenValue + eightValue + nineValue + tenValue
                        + one1Value + two1Value + three1Value + four1Value + five1Value + six1Value + seven1Value + eight1Value + nine1Value + ten1Value;
                JSONObject jsonObject = new JSONObject();
                //IEIM号
                //jsonObject.put("number", number);
                //二维码id
                jsonObject.put("number", deviceIdJiXiAfter);
                jsonObject.put("iotNumber", value);
                jsonObject.put("ieimCode", number);
                //rest
                //return fireFightingiot(jsonObject,"firefightingiot","updateIotNumber","device");
                Integer count = this.pananSmockNumberMapper.selectCount(new QueryWrapper<PananSmockNumber>().eq("device_number", deviceIdJiXiAfter));
                if (count <= 0) {
                    PananSmockNumber pananSmockNumber = new PananSmockNumber();
                    pananSmockNumber.put("id", UUID.randomUUID().toString());
                    pananSmockNumber.put("ieimNumber", number);
                    pananSmockNumber.put("iotNumber", value);
                    pananSmockNumber.put("deviceNumber", deviceIdJiXiAfter);
                    pananSmockNumberMapper.insert(pananSmockNumber);
                }
            }

        } else if (instructions.equals("01")) {
            //指令类型
            //报警内容content：0101045f
            String instructionsType = AlarmContent.substring(2, 4);
            if (instructionsType.equals("01")) {
                //信号强度
                String signalIntensity = AlarmContent.substring(4, 6);
                //电量
                String electricQuantity = AlarmContent.substring(6, 8);
                JSONObject jsonParamN = new JSONObject();
                //消防数据
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", "烟雾浓度变化");
                jsonObject.put("value", null);
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(jsonObject);
                //平时数据
                jsonParamN.put("interfaceType", "2");
                jsonParamN.put("deviceClassify", "3");
                jsonParamN.put("number", number);
                jsonParamN.put("time", time);
                jsonParamN.put("paraminfo", jsonArray);
                //正常原始数据存储
                SmokeNormalDeviceDataChanged smokeNormalDeviceDataChanged = new SmokeNormalDeviceDataChanged();
                smokeNormalDeviceDataChanged.setId(UUID.randomUUID().toString());
                smokeNormalDeviceDataChanged.setJsondata(jsonParam.toString());
                smokeNormalDeviceDataChangedMapper.insert(smokeNormalDeviceDataChanged);
                //正常数据
                jsonParamN.put("dataType", "2");
                fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
                fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
                //异步更新设备状态
                jsonParamN.put("state", "1");
                asyncService.updateDeviceCurrentState(jsonParamN);
            }
        }
        return RespEntity.success();

    }

    /**
     * @Author YangYu
     * @Description 三江烟感
     * @Date Created in 16:05 2022/10/26
     * @Param [jsonParam]
     */

    public RespEntity sanjiangSmoke(JSONObject jsonParam) throws Exception {
        //设备号
        String number = jsonParam.getJSONObject("ext").getStr("IMEI");
        //时间
        String time = jsonParam.getStr("at");
        //报警数据
        String data = jsonParam.getStr("data");
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        //报警标识
        boolean flag = false;
        if (data.equals("烟感传感器火警")) {
            alarmTypeName = "烟感火警";
            content = "烟感火警";
            flag = true;
        } else if (data.equals("设备自检")) {
            alarmTypeName = "自检报警";
            content = "自检报警";
            flag = true;
        } else if (data.equals("[告警]拆除报警")) {
            alarmTypeName = "拆除报警";
            content = "拆除报警";
            flag = true;
        } else if (data.equals("[提醒]通信故障")) {
            alarmTypeName = "烟感故障";
            content = "通信故障";
            flag = true;
        } else if (data.equals("[告警]低电压告警")) {
            alarmTypeName = "低电量报警";
            content = "低电压告警";
            flag = true;
        } else if (data.equals("[告警]传感器故障")) {
            alarmTypeName = "烟感故障";
            content = "传感器故障";
            flag = true;
        } else if (data.equals("温感火警")) {
            alarmTypeName = "烟感故障";
            content = "传感器故障";
            flag = true;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        if (flag) {
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            //rest
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        //报警标识
        boolean normalFlag = false;
        //值
        String value = null;
        if (data.equals("设备心跳")) {
            normalFlag = true;
        }
        if (jsonParam.getStr("streamId").equals("smokeScope")) {
            value = data;
            normalFlag = true;
        }
        if (normalFlag) {
            JSONObject paraminfo = new JSONObject();
            paraminfo.put("name", "烟雾浓度变化");
            paraminfo.put("value", value);
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(paraminfo);
            jsonObject.put("paraminfo", jsonArray);
            jsonObject.put("time", time);
            //正常数据
            jsonObject.put("dataType", "2");
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            fireFightingiot(jsonObject, "firefightingiot_data", "normalInfo", "device");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 三江烟感(电信)
     * @Date Created in 16:05 2022/10/26
     * @Param [jsonParam]
     */

    public RespEntity sanJiangSmokeTelecom(JSONObject jsonParam) throws Exception {
        //数据类型
        String messageType = jsonParam.getStr("messageType");
        if (!messageType.equals("dataReport")) {
            return RespEntity.success();
        }
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //数据
        String data = convertHexToString(HexUtil.encodeHexStr(Base64Decoder.decode(jsonParam.getJSONObject("payload").getStr("APPdata"))));
        if (!data.contains("5A")) {
            return RespEntity.fail("数据格式不对");
        }
        //5A030B0B000B0B1D0F0B110B0B0B0B0B53,20230410054135,100,-800,180,0,07729D52,18
        //5A020EDE0E86,20230422022111,100,-790,90,0,0945FC52,19,2508,224
        //序号
        Integer serialNumber = hexToInt(data.substring(4, 6));
        //命令类型
        String cmd = yiHuo(data.substring(6, 8), serialNumber);

        //数据
        String[] code = data.split(",");
        //解析数据
        String parsingData = code[0];
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        //烟雾浓度
        double smokeConcentration = 0;
        //温度
        Integer temperature = 0;
        //电池电量
        String battery = code[2];
        //CSQ信号强度
        String csq = code[7];
        //报警标识
        boolean flag = false;
        if (cmd.equals("0D")) {
            alarmTypeName = "自检报警";
            content = "自检报警";
            flag = true;
        } else if (cmd.equals("01")) {
            alarmTypeName = "烟感火警";
            smokeConcentration = hexToInt(yiHuo(parsingData.substring(10, 12), serialNumber));
            temperature = hexToInt(yiHuo(parsingData.substring(12, 14), serialNumber));
            content = "烟感火警 信号强度:" + csq + " 电池余量:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
            flag = true;
        } else if (cmd.equals("02")) {
            alarmTypeName = "烟感故障";
            smokeConcentration = hexToInt(yiHuo(parsingData.substring(10, 12), serialNumber));
            content = "传感器故障 信号强度:" + csq + " 电池余量:" + battery + " 烟雾浓度:" + smokeConcentration;
            flag = true;
        } else if (cmd.equals("03")) {
            alarmTypeName = "低电量报警";
            content = "低压故障" + " 电池余量:" + battery;
            flag = true;
        } else if (cmd.equals("0A")) {
            alarmTypeName = "拆除报警";
            content = "烟感拆除";
            flag = true;
        } else if (cmd.equals("10")) {
            alarmTypeName = "烟感温度报警";
            content = "温感火警 信号强度:" + csq + " 电池余量:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
            flag = true;
        } else if (cmd.equals("12")) {
            alarmTypeName = "烟感故障";
            content = "温度传感器故障" + " 温度:" + temperature;
            flag = true;
        } else if (cmd.equals("00")) {
            temperature = hexToInt(yiHuo(parsingData.substring(18, 20), serialNumber));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        if (flag) {
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            //rest
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        JSONObject jsonParamN = new JSONObject();
        //消防数据
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(getNormalItem("烟雾浓度变化", smokeConcentration));
        jsonArray.add(getNormalItem("温度", temperature));
        jsonArray.add(getNormalItem("电量", battery));
        jsonArray.add(getNormalItem("信号", csq));
        //平时数据
        jsonParamN.put("number", number);
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        //正常数据
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 淮安互联通烟感(驰通达)
     * @Date Created in 16:05 2022/10/26
     * @Param [jsonParam]
     */

    public RespEntity chitongda(JSONObject jsonParam) throws Exception {
        try {
            if (!jsonParam.containsKey("payload")) {
                return RespEntity.success();
            }
            //设备号
            String number = jsonParam.getStr("IMEI");
            //时间
            String time = contextLoads(jsonParam.getLong("timestamp"));
            //报警数据
            String data = convertHexToString(HexUtil.encodeHexStr(Base64Decoder.decode(jsonParam.getJSONObject("payload").getStr("APPdata"))));
            //命令位
            String commandBit = data.substring(6, 8);
            //报警类型名称
            String alarmTypeName = "";
            //报警内容
            String content = "";
            //烟雾浓度
            double smokeConcentration = 0;
            //温度
            Integer temperature = 0;
            //电池电量
            double battery = 0;
            //CSQ信号强度
            Integer csq = 0;
            //转发数据
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("number", number);
            //设备注册
            if (commandBit.equals("06")) {
                jsonObject.put("state", "1");
                asyncService.updateDeviceCurrentState(jsonObject);
                jsonObject.put("iccid", data.substring(38, 58));
                this.asyncService.update_ICCID(jsonObject);
                return RespEntity.success();
                //设备报警 AA5502030A10000E001700990D0000EA
            } else if (commandBit.equals("03")) {
                //烟雾浓度
                String smokeConcentrationStr = String.format("%.2f", (0.15 / 1100) * ByteUtil.hex16To10(data.substring(16, 18) + data.substring(14, 16)));
                smokeConcentration = Double.parseDouble(smokeConcentrationStr);
                //温度
                temperature = ByteUtil.hex16To10(data.substring(20, 22) + data.substring(18, 20));
                //电池电量
                String batteryStr = String.format("%.2f", ByteUtil.hex16To10(data.substring(24, 26) + data.substring(22, 24)) / 1000.0);
                battery = Double.parseDouble(batteryStr);
                //CSQ信号强度
                csq = ByteUtil.hex16To10(data.substring(26, 28));
                //00010000
                //设备状态位AA5502030A10000E001700990D0000EA
                String state = hexToBinaryString(data.substring(12, 14) + data.substring(10, 12));
                boolean flag = false;
                if (state.charAt(3) == '1') {
                    alarmTypeName = "自检报警";
                    content = "自检报警 信号强度:" + csq + " 电池电压:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
                    flag = true;
                } else if (state.charAt(4) == '1') {
                    alarmTypeName = "烟感故障";
                    content = "故障报警 信号强度:" + csq + " 电池电压:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
                    flag = true;
                } else if (state.charAt(5) == '1') {
                    alarmTypeName = "烟感温度报警";
                    content = "温度报警 信号强度:" + csq + " 电池电压:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
                    flag = true;
                } else if (state.charAt(6) == '1') {
                    alarmTypeName = "低电量报警";
                    content = "电池低压 信号强度:" + csq + " 电池电压:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
                    flag = true;
                } else if (state.charAt(7) == '1') {
                    alarmTypeName = "烟感火警";
                    content = "烟雾报警 信号强度:" + csq + " 电池电压:" + battery + " 温度:" + temperature + " 烟雾浓度:" + smokeConcentration;
                    flag = true;
                }
                if (flag) {
                    jsonObject.put("deviceClassify", "3");
                    jsonObject.put("interfaceType", "1");
                    jsonObject.put("alarmtime", time);
                    jsonObject.put("alarmTypeName", alarmTypeName);
                    jsonObject.put("content", content);
                    //rest
                    fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
                    //异步更新设备状态
                    jsonObject.put("state", "2");
                    asyncService.updateDeviceCurrentState(jsonObject);
                }
                // 心跳数据AA55020214000000001600 A40D509F700D4E8054010600001488
            } else if (commandBit.equals("02")) {
                //烟雾浓度
                String smokeConcentrationStr = String.format("%.2f", (0.15 / 1100) * ByteUtil.hex16To10(data.substring(16, 18) + data.substring(14, 16)));
                smokeConcentration = Double.parseDouble(smokeConcentrationStr);
                //温度
                temperature = ByteUtil.hex16To10(data.substring(20, 22) + data.substring(18, 20));
                //电池电量
                String batteryStr = String.format("%.2f", ByteUtil.hex16To10(data.substring(24, 26) + data.substring(22, 24)) / 1000.0);
                battery = Double.parseDouble(batteryStr);
                //CSQ信号强度
                csq = ByteUtil.hex16To10(data.substring(48, 50));
                jsonObject.put("state", "1");
                asyncService.updateDeviceCurrentState(jsonObject);
            }
            //电池低于2.7v状态更新为故障
            if (battery < 2.7) {
                jsonObject.put("state", "4");
                asyncService.updateDeviceCurrentState(jsonObject);
            }
            JSONObject jsonParamN = new JSONObject();
            //消防数据
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(getNormalItem("烟雾浓度变化", smokeConcentration));
            jsonArray.add(getNormalItem("温度", temperature));
            jsonArray.add(getNormalItem("电量", battery));
            jsonArray.add(getNormalItem("信号", csq));
            //平时数据
            jsonParamN.put("interfaceType", "2");
            jsonParamN.put("deviceClassify", "3");
            jsonParamN.put("number", number);
            jsonParamN.put("time", time);
            jsonParamN.put("paraminfo", jsonArray);
            //正常数据
            jsonParamN.put("dataType", "2");
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
            fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
            System.out.println("报警发送到正常数据层");
        } catch (Exception e) {
            System.out.println("------Exception------" + e);
        }

        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 大华烟感（电信）
     * @Date Created in 14:57 2024/4/13
     * @Param [jsonParam]
     */

    public RespEntity daHuaSmoke(JSONObject jsonParam) throws Exception {
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", jsonParam.getStr("IMEI"));
        //时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //如果不是该数据类型直接抛掉
        if (!jsonParam.getStr("messageType").equals("dataReport")) {
            jsonParamN.put("state", 1);
            // 异步更新设备状态
            asyncService.updateDeviceCurrentState(jsonParamN);
            return RespEntity.success();
        }

        //数据
        JSONObject payload = jsonParam.getJSONObject("payload");
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        //报警标识
        boolean flag = false;
        String smokeValue = null;
        if (payload.containsKey("IMEI")) {
            jsonParamN.put("iccid", payload.getStr("ICCID"));
            // 异步更新物联网卡号
            asyncService.update_ICCID(jsonParamN);
            jsonParamN.put("state", 1);
            // 异步更新设备状态
            asyncService.updateDeviceCurrentState(jsonParamN);
            return RespEntity.success();
        } else if (payload.containsKey("operation")) {
            String operation = payload.getStr("operation");
            if (operation.equals("1")) {
                alarmTypeName = "自检报警";
                content = alarmTypeName;
                flag = true;
            }
        } else if (payload.containsKey("smoke_state")) {
            String smoke_state = payload.getStr("smoke_state");
            if (smoke_state.equals("0")) {
                alarmTypeName = "烟雾报警";
                content = alarmTypeName + " 报警值：" + payload.getStr("smoke_value");
                flag = true;
                smokeValue = payload.getStr("smoke_value");
            }

        } else if (payload.containsKey("error")) {
            String error = payload.getStr("error");
            if (error.equals("1")) {
                alarmTypeName = "烟感故障";
                content = alarmTypeName;
                flag = true;
            } else if (error.equals("3")) {
                alarmTypeName = "低电量报警";
                content = "低压故障";
                flag = true;
            }
        } else if (payload.containsKey("tamper_alarm")) {
            String tamper_alarm = payload.getStr("tamper_alarm");
            if (tamper_alarm.equals("1")) {
                alarmTypeName = "拆除报警";
                content = alarmTypeName;
                flag = true;
            }
        }
        //烟雾报警
        if (flag) {
            jsonParamN.put("deviceClassify", "3");
            jsonParamN.put("interfaceType", "1");
            jsonParamN.put("alarmtime", time);
            jsonParamN.put("alarmTypeName", alarmTypeName);
            jsonParamN.put("content", content);
            //rest
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        }
        //消防数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "烟雾浓度变化");
        jsonObject.put("value", smokeValue);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        //平时数据
        jsonParamN.put("interfaceType", "2");
        jsonParamN.put("deviceClassify", "3");
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 大华烟感（电信4G-DH-HY-SA2GA-A）
     * @Date Created in 2024/8/5
     * @Param [jsonParam]
     */

    public RespEntity daHuaSmoke4g(JSONObject jsonParam) throws Exception {
        if (jsonParam.getStr("messageType").equals("dataReport") || jsonParam.getStr("messageType").equals("eventReport")) {
            // topic
            String serviceId = jsonParam.getStr("serviceId");
            JSONObject jsonParamN = new JSONObject();
            //报警类型名称
            String alarmTypeName = "";
            //时间
            String time = contextLoads(jsonParam.getLong("timestamp"));
            //报警内容
            String content = "";
            //报警标识
            boolean flag = false;
            //保留两位小数
            DecimalFormat decimalFormat = new DecimalFormat("#.##");
            //温度
            String temperature_value = "0";
            //信号
            String signalPower = "";
            //电量
            String battery_value = "";
            //烟雾浓度
            String smoke_value = "0";
            // 注册
            if (serviceId.equals("register_report")) {
                //数据
                JSONObject payload = jsonParam.getJSONObject("payload");
                jsonParamN.put("number", jsonParam.getStr("IMEI"));
                jsonParamN.put("iccid", payload.getStr("ICCID"));
                // 异步更新物联网卡号
                asyncService.update_ICCID(jsonParamN);
                jsonParamN.put("state", 1);
                // 异步更新设备状态
                asyncService.updateDeviceCurrentState(jsonParamN);
                return RespEntity.success();
            } else if (serviceId.equals("operation_report")) {
                //数据
                JSONObject payload = jsonParam.getJSONObject("payload");
                jsonParamN.put("number", jsonParam.getStr("deviceId").substring(8));
                //操作上报
                String operation = payload.getStr("operation");
                //自检
                if (operation.equals("1")) {
                    //温度
                    temperature_value = decimalFormat.format(payload.getDouble("temperature_value"));
                    //信号
                    signalPower = decimalFormat.format(payload.getDouble("signalPower"));
                    //电量
                    battery_value = decimalFormat.format(payload.getDouble("battery_value"));
                    alarmTypeName = "自检报警";
                    content = alarmTypeName;
                    flag = true;
                } else if (operation.equals("5")) {
                    //温度
                    temperature_value = decimalFormat.format(payload.getDouble("temperature_value"));
                    //信号
                    signalPower = decimalFormat.format(payload.getDouble("signalPower"));
                    //电量
                    battery_value = decimalFormat.format(payload.getDouble("battery_value"));
                    alarmTypeName = "自检报警";
                    content = "消音";
                    flag = true;
                }
            } else if (serviceId.equals("smoke_report")) {
                //数据
                JSONObject payload = jsonParam.getJSONObject("payload");
                jsonParamN.put("number", jsonParam.getStr("deviceId").substring(8));
                //烟雾报警状态
                String smoke_state = payload.getStr("smoke_state");
                //烟雾报警
                if (smoke_state.equals("0")) {
                    //信号
                    signalPower = decimalFormat.format(payload.getDouble("signalPower"));
                    //电量
                    battery_value = decimalFormat.format(payload.getDouble("battery_value"));
                    //烟雾浓度
                    smoke_value = decimalFormat.format(payload.getDouble("smoke_value"));
                    alarmTypeName = "烟雾报警";
                    content = alarmTypeName + " 报警值：" + payload.getStr("smoke_value");
                    flag = true;
                }
            } else if (serviceId.equals("devicepara_report")) {
                //数据
                JSONObject payload = jsonParam.getJSONObject("payload");
                jsonParamN.put("number", jsonParam.getStr("deviceId").substring(8));
                //温度
                temperature_value = decimalFormat.format(payload.getDouble("temperature_value"));
                //信号
                signalPower = decimalFormat.format(payload.getDouble("signalPower"));
                //电量
                battery_value = decimalFormat.format(payload.getDouble("battery_value"));
            } else if (serviceId.equals("remove_report")) {
                //数据
                JSONObject eventContent = jsonParam.getJSONObject("eventContent");
                jsonParamN.put("number", jsonParam.getStr("deviceId").substring(8));
                //拆除报警状态
                String tamper_alarm = eventContent.getStr("tamper_alarm");
                if (tamper_alarm.equals("1")) {
                    alarmTypeName = "拆除报警";
                    content = "设备拆除";
                    flag = true;
                } else if (tamper_alarm.equals("0")) {
                    alarmTypeName = "拆除报警";
                    content = "设备装上";
                    flag = true;
                }
            }
            //烟雾报警
            if (flag) {
                jsonParamN.put("deviceClassify", "3");
                jsonParamN.put("interfaceType", "1");
                jsonParamN.put("alarmtime", time);
                jsonParamN.put("alarmTypeName", alarmTypeName);
                jsonParamN.put("content", content);
                //rest
                fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
            }
            //平时数据
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(getNormalItem("烟雾浓度变化", smoke_value));
            jsonArray.add(getNormalItem("信号", signalPower));
            jsonArray.add(getNormalItem("电量", battery_value));
            jsonParamN.put("interfaceType", "2");
            jsonParamN.put("deviceClassify", "3");
            jsonParamN.put("time", time);
            jsonParamN.put("paraminfo", jsonArray);
            fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        } else {
            return RespEntity.success();
        }
        return RespEntity.success();
    }



    public HashMap<String, String> AscIITable() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("20", " ");
        hashMap.put("21", "!");
        hashMap.put("22", "\"");
        hashMap.put("23", "#");
        hashMap.put("24", "$");
        hashMap.put("25", "%");
        hashMap.put("26", "&");
        hashMap.put("27", "'");
        hashMap.put("28", "(");
        hashMap.put("29", ")");
        hashMap.put("2A", "*");
        hashMap.put("2B", "+");
        hashMap.put("2C", ",");
        hashMap.put("2D", "-");
        hashMap.put("2E", ".");
        hashMap.put("2F", "/");
        hashMap.put("30", "0");
        hashMap.put("31", "1");
        hashMap.put("32", "2");
        hashMap.put("33", "3");
        hashMap.put("34", "4");
        hashMap.put("35", "5");
        hashMap.put("36", "6");
        hashMap.put("37", "7");
        hashMap.put("38", "8");
        hashMap.put("39", "9");
        hashMap.put("3A", ":");
        hashMap.put("3B", ";");
        hashMap.put("3C", "<");
        hashMap.put("3D", "=");
        hashMap.put("3E", ">");
        hashMap.put("3F", "?");
        hashMap.put("40", "@");
        hashMap.put("41", "A");
        hashMap.put("42", "B");
        hashMap.put("43", "C");
        hashMap.put("44", "D");
        hashMap.put("45", "E");
        hashMap.put("46", "F");
        hashMap.put("47", "G");
        hashMap.put("48", "H");
        hashMap.put("49", "I");
        hashMap.put("4A", "J");
        hashMap.put("4B", "K");
        hashMap.put("4C", "L");
        hashMap.put("4D", "M");
        hashMap.put("4E", "N");
        hashMap.put("4F", "O");
        hashMap.put("50", "P");
        hashMap.put("51", "Q");
        hashMap.put("52", "R");
        hashMap.put("53", "S");
        hashMap.put("54", "T");
        hashMap.put("55", "U");
        hashMap.put("56", "V");
        hashMap.put("57", "W");
        hashMap.put("58", "X");
        hashMap.put("59", "Y");
        hashMap.put("5A", "Z");
        hashMap.put("5B", "[");
        hashMap.put("5C", "\\");
        hashMap.put("5D", "]");
        hashMap.put("5E", "^");
        hashMap.put("5F", "_");
        hashMap.put("60", "`");
        hashMap.put("61", "a");
        hashMap.put("62", "b");
        hashMap.put("63", "c");
        hashMap.put("64", "d");
        hashMap.put("65", "e");
        hashMap.put("66", "f");
        hashMap.put("67", "g");
        hashMap.put("68", "h");
        hashMap.put("69", "i");
        hashMap.put("6A", "j");
        hashMap.put("6B", "k");
        hashMap.put("6C", "l");
        hashMap.put("6D", "m");
        hashMap.put("6E", "n");
        hashMap.put("6F", "o");
        hashMap.put("70", "p");
        hashMap.put("71", "q");
        hashMap.put("72", "r");
        hashMap.put("73", "s");
        hashMap.put("74", "t");
        hashMap.put("75", "u");
        hashMap.put("76", "v");
        hashMap.put("77", "w");
        hashMap.put("78", "x");
        hashMap.put("79", "y");
        hashMap.put("7A", "z");
        hashMap.put("7B", "{");
        hashMap.put("7C", "|");
        hashMap.put("7D", "}");
        hashMap.put("7E", "~");
        return hashMap;
    }

    //派安烟感对接测试
    public RespEntity smokeanpaiceshi(JSONObject jsonParam) throws Exception {
        //报警原始数据存储
        Smokeanpaiceshi smokeanpaiceshi = new Smokeanpaiceshi();
        smokeanpaiceshi.setId(UUID.randomUUID().toString());
        smokeanpaiceshi.setJson(jsonParam.toString());
        smokeanpaiceshiMapper.insert(smokeanpaiceshi);
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 校验数字是技术还是偶数
     * @Date Created in 15:47 2023/4/22
     * @Param [a]
     */

    public static String oddOrEven(int a) {
        if (a % 2 != 0) {
            return "奇数";
        } else {
            return "偶数";
        }
    }

    /**
     * @Author YangYu
     * @Description 十六进制转十进制
     * @Date Created in 16:37 2022/1/12
     * @Param [hex]
     */

    public static int hexToInt(String hex) {
        if (hex.length() < 1) {
            return 0;
        } else {
            return parseInt(hex, 16);
        }
    }

    /**
     * 将毫秒转化为日期(格式:yyyy-MM-dd hh:mm:ss)
     *
     * @param l
     * @return
     * @throws Exception
     */
    public String contextLoads(Long l) throws Exception {

        java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        Date date = new Date(l);
        String str1 = sdf1.format(date);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = sdf.parse(str1);
        String str2 = sdf.format(date2);
        return str2;
    }

    /**
     * 16进制 转 ASCII码
     *
     * @param “3030313030312d30303300000000000000000000”
     * @return 001001-003
     */
    public static String convertHexToString(String hex) {
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for (int i = 0; i < hex.length() - 1; i += 2) {

            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            if (!output.equals("00")) {
                //convert hex to decimal
                int decimal = parseInt(output, 16);
                //convert the decimal to character
                sb.append((char) decimal);

                temp.append(decimal);
            }
        }

        return sb.toString();

    }

    /**
     * 十六进制异或
     */
    public static String yiHuo(String source, int i) {
        Integer hexToInt = hexToInt(source);
        String hex = Integer.toHexString(hexToInt ^ i);
        //奇数偶数校验
        String oddOrEven = oddOrEven(i);
        if (oddOrEven.equals("偶数")) {
            hex = hex.substring(1, 2) + hex.substring(0, 1);
        }
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex.toUpperCase();
    }


    /**
     * 获取数据
     *
     * @return
     */
    public static JSONObject getHkData(JSONObject jsonParam) {
        // 数据格式化
        List<String> strings = formatData(jsonParam.getJSONObject("payload").getStr("msg"));
        System.out.println(strings);
        JSONObject jsonObject = new JSONObject();
        // 业务类型
        String byMessageId = getInfo(strings, 1, 1);
        // 扩展位
        String byFixedSign = getInfo(strings, 2, 1);
        // 设备类型低八位
        String byDevType = getInfo(strings, 3, 1);
        // Mac地址
        String byMac = getInfo(strings, 4, 6);
        // 时间
        String byTime = getInfo(strings, 10, 4);
        // 设备类型高八位
        String byDevTypeEx = getInfo(strings, 14, 1);
        // 小区编号
        String wPci = getInfo(strings, 15, 2);
        // 网络质量参数
        String bySnr = getInfo(strings, 17, 1);
        // 网络质量参数2
        String byEcl = getInfo(strings, 18, 1);
        // 网络质量参数3
        String wRsrp = getInfo(strings, 19, 2);
        // 上报协议头长度
        String dwUpHeaderLen = getInfo(strings, 21, 4);
        // 包序号
        String dwPackageNo = getInfo(strings, 25, 4);
        // QCCID号
        String byQccId = getInfo(strings, 29, 20);
        // IMEI号
        String byImei = getInfo(strings, 49, 20);
        // IMSI号
        String byImsi = getInfo(strings, 69, 20);
        // NB模块版本
        String byNbModuleVersion = getInfo(strings, 89, 24);
        // 基站码
        String dwCid = getInfo(strings, 113, 4);
        // 地区区域码
        String dwLac = getInfo(strings, 117, 4);
        // 软件版本
        String bySoftwareVersion = getInfo(strings, 121, 20);
        // 硬件版本
        String byHardwareVersion = getInfo(strings, 141, 20);
        // 设备型号
        String byDeciceModel = getInfo(strings, 161, 20);
        // 协议版本
        String byProtocolVersion = getInfo(strings, 181, 10);

        // UP_HEADER 上行通道头部分
        jsonObject.putOnce("上行通道头部分", "---------");
        jsonObject.putOnce("byMessageId", byMessageId);
        jsonObject.putOnce("byFixedSign", byFixedSign);
        jsonObject.putOnce("byDevType", byDevType);
        jsonObject.putOnce("byMac", byMac);
        jsonObject.putOnce("byTime", byTime);
        jsonObject.putOnce("byDevTypeEx", byDevTypeEx);
        jsonObject.putOnce("wPci", wPci);
        jsonObject.putOnce("bySnr", bySnr);
        jsonObject.putOnce("byEcl", byEcl);
        jsonObject.putOnce("wRsrp", wRsrp);
        jsonObject.putOnce("dwUpHeaderLen", dwUpHeaderLen);
        jsonObject.putOnce("dwPackageNo", dwPackageNo);
        jsonObject.putOnce("byQccId", byQccId);
        jsonObject.putOnce("byImei", byImei);
        jsonObject.putOnce("byImsi", byImsi);
        jsonObject.putOnce("byNbModuleVersion", byNbModuleVersion);
        jsonObject.putOnce("dwCid", dwCid);
        jsonObject.putOnce("dwLac", dwLac);
        jsonObject.putOnce("bySoftwareVersion", bySoftwareVersion);
        jsonObject.putOnce("byHardwareVersion", byHardwareVersion);
        jsonObject.putOnce("byDeciceModel", byDeciceModel);
        jsonObject.putOnce("byProtocolVersion", byProtocolVersion);

        // MULTI_CHAN_HEADER 多消息通道头部分
        // 保留字段
        String byRes = getInfo(strings, 191, 1);
        // 消息类型
        String byMsgType = getInfo(strings, 192, 1);
        // 屏蔽状态
        String byShield = getInfo(strings, 193, 1);
        // 多通道版本
        String byMultiChanBodyVersion = getInfo(strings, 194, 1);
        // 负载数据长度
        String wLen = getInfo(strings, 195, 2);
        // 通道类型
        String wChanTscType = getInfo(strings, 197, 2);
        // 通道值
        String wChanRscValue = getInfo(strings, 199, 2);

        jsonObject.putOnce("多消息通道头部分", "--------");
        jsonObject.putOnce("byRes", byRes);
        jsonObject.putOnce("byMsgType", byMsgType);
        jsonObject.putOnce("byShield", byShield);
        jsonObject.putOnce("byMultiChanBodyVersion", byMultiChanBodyVersion);
        jsonObject.putOnce("wLen", wLen);
        jsonObject.putOnce("wChanTscType", wChanTscType);
        jsonObject.putOnce("wChanRscValue", wChanRscValue);

        // 判断通道数
        Integer chanNumbers = hexToInt(wChanRscValue);

        // 循环取消息体
        for (int i = 0; i < chanNumbers; i++) {
            JSONObject bodyInfo = new JSONObject();
            int start = 201 + 14 * i;

            // 类型
            String wType = getInfo(strings, start, 2); //201
            // 值
            String wValue = getInfo(strings, start + 2, 2);
            // 事件类型
            String wEventType = getInfo(strings, start + 4, 2);
            // 事件值高16位
            String wEventValueHight = getInfo(strings, start + 6, 2);
            // 事件值低16位
            String wEventValue = getInfo(strings, start + 8, 2);
            // 参数类型
            String wParamType = getInfo(strings, start + 10, 2);
            // 参数值
            String wParamValue = getInfo(strings, start + 12, 2); // 213
            if (i == 1 && wParamType.equals("0038") && wParamValue.equals("ffff")) {
                String length = getInfo(strings, start + 14, 2);
                String byLength = getInfo(strings, start + 16, 1);
                String bySleepTime = getInfo(strings, start + 17, 1);
                String byTAUTime = getInfo(strings, start + 18, 1);
                bodyInfo.putOnce("byLength", byLength);
                bodyInfo.putOnce("bySleepTime", bySleepTime);
                bodyInfo.putOnce("byTAUTime", byTAUTime);
            }

            bodyInfo.putOnce("wType", wType);
            bodyInfo.putOnce("wValue", wValue);
            bodyInfo.putOnce("wEventType", wEventType);
            bodyInfo.putOnce("wEventValueHight", wEventValueHight);
            bodyInfo.putOnce("wEventValue", wEventValue);
            bodyInfo.putOnce("wParamType", wParamType);
            bodyInfo.putOnce("wParamValue", wParamValue);
            jsonObject.putOnce("bodyInfo" + (i + 1), bodyInfo);
        }

//
        // 校验和
        String checkCode = getInfo(strings, 201 + 14 * chanNumbers, 1);
        jsonObject.putOnce("checkCode", checkCode);
        return jsonObject;
    }


    /**
     * 格式化数据体
     */
    public static List<String> formatData(String data) {
        List<String> splitList = new ArrayList<>();
        for (int i = 0; i < data.length(); i += 2) {
            if (i + 1 < data.length()) {
                splitList.add(data.substring(i, i + 2));
            } else {
                splitList.add(data.substring(i));
            }
        }
        return splitList;
    }


    /**
     * 拿数据
     *
     * @param data   格式化的数据列表
     * @param index  第几个字节
     * @param lenght 取几个字节
     * @return 返回所需的字节，高低位反转
     */
    public static String getInfo(List<String> data, Integer index, Integer lenght) {
        if (index >= data.size() + 3) {
            return "error";
        }
        StringBuffer stringBuffer = new StringBuffer();
        if (lenght == 1) {
            for (int i = index; i < lenght + index; i++) {
                stringBuffer.append(data.get(i - 1));
            }
            return stringBuffer.toString();
        } else if (lenght > 1) {
            int i;
            for (i = index; i <= index + lenght - 1; i++) {
                stringBuffer.append(getInfo(data, i, 1));
            }
            return stringBuffer.toString();
        } else {
            return "error";
        }
    }

    /**
     * 16转ascii码
     */

    public String hexToAscii(String hexStr) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i += 2) {
            String str = hexStr.substring(i, i + 2);
            if ("00".equals(str)) {
                continue;
            }
            output.append((char) Integer.parseInt(str, 16));
        }
        return output.toString();
    }

}
